
//
// 130530 - AH - Created.
// 130531 - AH - Put 'seed socraton' on orb.
// 130607 - AH - fromSocratons(Object).
//

package org.soekrat.engine.socraton;

import java.io.IOException;

import org.soekrat.environment.Orb;

/**
 * <p>
 *   Object that creates
 *   {@link Orb} from a
 *   {@link Socraton}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class SocratonToOrb
{
  private final String m_context;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public SocratonToOrb(){ this("server"); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param context Name on context to initialize the 
   *                {@link Orb} from. Default is "server".
   */
  public SocratonToOrb(String context)
  {
    if (context==null)
    {
      throw new NullPointerException("context");
    }
    
    context=context.trim();
    if (context.length()==0)
    {
      throw new IllegalArgumentException("context the empty string");
    }
    
    m_context=context;
  }
  
  /**
   * <p>
   *   Gets context initialize 
   *   {@link Orb} from.
   * </p>
   * @return Context. Default is "server".
   */
  public String getContext(){ return m_context; }
  
  /**
   * <p>
   *   Creates
   *   {@link Orb} from a
   *   {@link Socraton}.
   * </p>
   * @param s {@code Socraton}.
   * @return Created.
   */
  public Orb toOrb(Socraton s)
  {
    if (s==null)
    {
      throw new NullPointerException("s");
    }
    
    Orb orb=new Orb();
    
    orb.setAttribute("seed-socraton", s);
    
    Bison[] bisons=s.getBisons(m_context);
    for (Bison bison : bisons)
    {
      ResolveBisonDataOLD.resolve(orb, bison);
    }
    
    return orb;
  }
  
  /**
   * <p>
   *   Create
   *   {@link Orb} from
   *   {@link Socraton} identified in pool and initialize with sensor data.  
   * </p>
   * @param id Id of socraton (read from XML) that represents 
   *           {@code Orb}.
   * @return   Created and initialized 
   *           {@code Orb}.
   * @throws IOException If fails.
   */
  public Orb fromSocratons(Object id) throws IOException
  {
    id=(id==null) ? "serverKit" : id;
    
    //
    // Gets local Socratons pool.
    //
    SocratonsService ss=SocratonsService.getInstance();
    Socratons socs=ss.getSocratons();
    
    //
    // Gets Socraton that represents Orb and create Orb from it.
    //
    Socraton seed=socs.getOrCreateWithId(id);
    Orb orb=toOrb(seed);
    
    Socraton sk=(id.equals("serverKit")) ? seed :
                socs.getOrCreateWithId("serverKit");
    
    //
    // Resolves the server kit socraton and put resolved on Orb. This should
    // put sensors on orb.
    //
    Bison[] sbisons=sk.getBisons("server");
    for (Bison bison : sbisons)
    {
      ResolveBisonDataOLD.resolve(orb, bison);
    }
    
    return orb;
  }
  
  private static Object[] fetchDataToSense()
  {
    return new Object[]{}; // Proto
  }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();
    sb.append("context=").append(m_context);
    return sb.toString();
  }
  
}
